package com.main;


import com.bean.Player;
import com.bean.Profession;
import com.bean.Team;

import java.util.*;
import java.util.stream.Collectors;

public class Bootstrap {

    public static List<Profession> allHps = new ArrayList<Profession>(){{
        add(new Profession("奶秀",true));
        add(new Profession("奶花",true));
        add(new Profession("奶毒",true));
        add(new Profession("奶歌",true));
    }};


    public static List<Profession> allProfession = new ArrayList<Profession>(){{

        add(new Profession("奶秀",true));
        add(new Profession("奶花",true));
        add(new Profession("奶毒",true));
        add(new Profession("奶歌",true));

        add(new Profession("花间",false));
        add(new Profession("冰心",false));
        add(new Profession("和尚",false));
        add(new Profession("剑纯",false));
        add(new Profession("气纯",false));
        add(new Profession("天策",false));
        add(new Profession("藏剑",false));
        add(new Profession("毒经",false));
        add(new Profession("鲸鱼",false));
        add(new Profession("田螺",false));
        add(new Profession("明教",false));
        add(new Profession("丐帮",false));
        add(new Profession("苍云",false));
        add(new Profession("莫问",false));
        add(new Profession("霸刀",false));
        add(new Profession("蓬莱",false));
        add(new Profession("凌雪",false));
        add(new Profession("衍天",false));

    }};



    public static void main(String[] args) {

        List<Player> playerList = new ArrayList<>();

        Map<String,String> data = new HashMap<>();
        data.put("顾久姒","苍云/丐帮/明教");
        data.put("风起青萍之末","奶花/奶秀/奶毒/奶歌/气纯/冰心");
        data.put("时光左左","藏剑/天策");
        data.put("和尚踏归","和尚");
        data.put("诗清欢","丐帮/奶花/霸刀");
        data.put("伊姆赫特","天策/明教");
        data.put("月笙歌","天策/明教/霸刀");
        data.put("复月","奶花/奶秀/奶毒/奶歌");
        data.put("小辞云","奶花/莫问");
        data.put("寒清","苍云");
        data.put("莫倾寒","霸刀");

        // 获取玩家信息
        for(String key:data.keySet()){//keySet获取map集合key的集合  然后在遍历key即可
            String value = data.get(key).toString();//

            Player player = new Player();
            // set名字
            player.setName(key);
            List<String> professions = Arrays.asList(value.split("/"));

            player.setProfessions(professions);
            playerList.add(player);
        }
        // 随机打乱list 的顺序
        Collections.shuffle(playerList);


        // 获取一共需要多少队伍
        int count = playerList.size() / 3;

        List<Team> teams = new ArrayList<>();
        List<Player> dps = new ArrayList<>();


        // 循环 玩家list 随机匹配心法
        for (Player player : playerList) {

            // 随机set 该玩家 使用的心法
            List<String> professions = player.getProfessions();
            // 过滤 该玩家 可以使用的心法
            List<Profession> willProfessions = allProfession.stream().filter(p ->
                    !professions.stream().collect(Collectors.toList())
                            .contains(p.getName())).collect(Collectors.toList());
            if(willProfessions.size() == 0){
                throw new RuntimeException(player.getName() + " 玩家心法有误");
            }
            List<Profession> whps = new ArrayList<>();
            // 判断 该人可以使用的所有治疗 心法
            for (Profession willProfession : willProfessions) {
                if (willProfession.isTreatment()){
                    whps.add(willProfession);
                }
            }
            // 随机 选取一个治疗心法
            if (whps.size() > 0 &&  count > 0){
                // 随机获取该玩家的 心法
                int i =(int) ((Math.random() * whps.size()));
                // 获取一个 随机的心法
                Profession profession = whps.get(i);
                player.setUseProfession(profession);
                Team team = new Team();
                // 加入奶妈组
                team.setHps(player);
                teams.add(team);
                count--;
            }else{
                // 这个人是DPS 把所有的 奶妈心法过滤
                List<Profession> willDps = willProfessions.stream().filter(p ->
                        !allHps.stream().map(a -> a.getName()).collect(Collectors.toList())
                                .contains(p.getName())).collect(Collectors.toList());
                int i =(int) ((Math.random() * willDps.size()));
                // 获取一个 随机心法
                Profession profession = willDps.get(i);
                // 给他一个 DPS 心法
                player.setUseProfession(profession);
                // 加入DPS组
                dps.add(player);


            }

        }

        // 循环奶妈 分组
        for (Team team : teams) {
            // 设置第一个DPS
            int i =(int) ((Math.random() * dps.size()));
            team.setDps1(dps.get(i));
            dps.remove(dps.get(i));
            // 设置第二个DPS
            int i2 =(int) ((Math.random() * dps.size()));
            team.setDps2(dps.get(i2));
            dps.remove(dps.get(i2));
        }

        int num  = 1 ;
        for (Team team : teams) {
            System.out.println("队伍 "+ num +":  " + team.getHps().getName() + "-" + team.getHps().getUseProfession().getName()  );
            System.out.println("队伍 "+ num +":  " + team.getDps1().getName() + "-" + team.getDps1().getUseProfession().getName()  );
            System.out.println("队伍 "+ num +":  " + team.getDps2().getName() + "-" + team.getDps2().getUseProfession().getName()  );
            num ++;
            System.out.println();
        }



        // 随机组队





    }
}
